<?php

// wf_create.php
// Michael Baker
// 2010.07.12
//
// Create workflow
// Complete with HORRIBLE JavaScript code!

session_start();

define("IN_MDM", true);
define("CUR_PAGE_TITLE", "Create workflow");
define("CUR_FLAG", "wf.create");
define("HEAD_INCLUDE_FILE", "crime.js.php");

include '../includes/all.php';
include 'inc/header.php';

if ( array_key_exists( "argc", $_POST ) ) {
  // we have an argument count variable. attempt to process

  $argc = intval( $_POST["argc"] );
  if ( !( $argc >= 2 ) ) {
    // Not enough arguments, corrupt data, etc.
    // Send user back to a blank form
    error_log( "wf_create received ARGC=$argc. Exiting (need at least ARGC=2 to proceed)" );
    header( "Location: wf_create.php" );
    die;
  }
  
  // Do processing
  $prefix = "n";
  $ctr = 1;
  $ip_arr = array();
    
  while ( $ctr <= $argc ) {
    $argName = $prefix . $ctr;
    
    if ( !array_key_exists( $argName, $_POST ) ) {
      // A parameter that SHOULD be present, according to argc, is absent.
      // Send user back to a blank form.
      error_log( "wf_create received ARGC=$argc, but failed to find item $argName" );
      header( "Location: wf_create.php" );
      die;
    }
    
    // It exists; retrieve its value
    $argVal = $_POST[ $argName ];

    if ( is_ip_address( $argVal ) ) {
      // we're dealing with IP address input. just push it into the array
      
      $type = 0;
      $ip_arr[] = $argVal;
    }
    else if ( is_numeric( $argVal ) ) {
      // we're dealing with node ID input. retrieve its IP and push it into the array
      
      $type = 1;
      try {
        $node = $dbase->get_node( $argVal );
      }
      catch (DatabaseException $e) {
        // invalid node ID supplied
        error_log("wf_create failed to locate node ID $argVal in database");
        header( "Location: wf_create.php" );
        die;
      }
      
      $ip_arr[] = $node->get_address();
    }
    else {
      // bizarre data - failed to match as either an IP or a node ID
      
      $type = -1;
      error_log("wf_create received bizarre data in node column $argName: $argVal");
      header( "Location: wf_create.php" );
      die;
    }
    
    $ctr++;
  }
  
  // This is a "fool-proof" wrapper for $dbase->create_workflow
  try {
    // this is a wrapper for $dbase->create_workflow that is "fool-proof"
    $info = ui_create_workflow( $ip_arr );
  }
  catch (Exception $e) {
    // WF could not be created - either a dead segment is present, etc
    // we should record the reason for the error, return the user to a blank form, with the
    // error message printed at the top.
    
    $_SESSION["MESSAGE"] = array( 1, $e->getMessage() );
    error_log($e->getMessage());
    header( "Location: wf_create.php" );
    die;
  }
  
  $has_wf_view = has_flag( $_SESSION["uid"], "wf.view" );
  $has_n_view  = has_flag( $_SESSION["uid"], "n.view"  );
  
  $wf_id       = $info[ 0 ];
  $node_id_arr = $info[ 1 ];
  $n_count     = count($node_id_arr);
  $nc_id_arr   = $info[ 2 ];
  $nc_count    = count($nc_id_arr);
  
  $str_nodes_t = "The following $n_count nodes had to be created: " . implode( ", ", $node_id_arr ) . ".";
  $str_nodes_f = "$n_count nodes had to be created.";
  
  $str_nodes   = ($n_count)  ? "<br />" . ( ($has_n_view) ? $str_nodes_t : $str_nodes_f ) : "";
  $str_nc      = ($nc_count) ? "<br />" . "Additionally, $nc_count segments were created." : "";
  
  $_SESSION["MESSAGE"] = array( 0, "Your <a href=\"wf_view.php?id=$wf_id\">workflow</a> has been created" . $str_nodes . $str_nc );
  header( "Location: wf_list.php" );
  die;
}

?>
<p>Please enter an ordered list of nodes. You may add either an existing node or a node by IP.</p>
<form action="wf_create.php" method="post">

<fieldset>	
  <p id="buttons">
  <input class="button2" onclick="addElement()" value="Add node from DB" type="button" /> &nbsp;
  <input class="button2" onclick="addElement2()" value="Add node by IP/ID" type="button" /> &nbsp;
  <input class="button2" onclick="dropElement()" value="Remove last node" type="button" /> &nbsp;
  </p>
  <div id="nodelist">
  </div>
  
  <p id="buttons2">
  <input type="submit" value="Create Workflow" class="button2" />
  <input type="hidden" value="0" name="argc" id="argc" />
  </p>
  
</fieldset>
</form>

<?php include 'inc/footer.php'; ?>